home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* */
- /* Application: Compression & Scaling */
- /* */
- /* Description: This snippet shows an example of how to convert a */
- /* version 2 PICT to a compressed QuickTime data buffer. */
- /* Once the PICT is compressed, it is then decompressed */
- /* to the window at one quarter its original size. */
- /* */
- /* Files: Compression & Scaling.π */
- /* Compression & Scaling.π.rsrc */
- /* Compression & Scaling.c */
- /* */
- /* Programmer: Edgar Lee */
- /* Organization: Apple Computer, Inc. */
- /* Department: Developer Technical Support, DTS */
- /* Language: C (Think C version 5.0.2) */
- /* Date Created: 10-20-92 */
- /* */
- /****************************************************************************/
-
- #include <ImageCompression.h>
- #include <Movies.h>
-
- /* Constant Declarations */
-
- #define WLEFT( x ) (((screenBits.bounds.right - screenBits.bounds.left) - x) / 2)
- #define WTOP( x ) (((screenBits.bounds.bottom - screenBits.bounds.top) - x) / 2)
-
- /* Global Variable Definitions */
-
- WindowPtr gWindow;
-
- void initMac();
- void checkForQuickTime();
- void doScaleTest();
- void createWindow();
-
- main()
- {
- initMac();
- checkForQuickTime();
-
- createWindow();
- doScaleTest();
-
- while (!Button());
- }
-
- void initMac()
- {
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( nil );
- InitCursor();
- FlushEvents( 0, everyEvent );
- }
-
- void checkForQuickTime()
- {
- long version;
-
- if (Gestalt( gestaltQuickTime, &version ) != noErr)
- {
- ParamText( "\pQuickTime not installed. Please install, then try again.", "\p", "\p", "\p" );
- Alert( 128, nil );
- ExitToShell();
- }
- }
-
- void createWindow()
- {
- Rect bounds = { 0, 0, 0, 0 };
-
- gWindow = NewCWindow( 0L, &bounds, "\pDecompression & Scaling", false, documentProc,
- (WindowPtr)-1L, false, 0L );
-
- SetPort( gWindow );
- }
-
- void doScaleTest()
- {
- int i;
- GWorldPtr srcGWorld;
- PixMapHandle srcPixMap;
- PicHandle pict;
- Rect pictBounds;
- Rect finalBounds;
- CGrafPtr savedPort;
- GDHandle savedDevice;
- short scaleRatio = 4;
-
- ImageDescriptionHandle desc;
- Ptr imageData;
- long size;
-
- /**********************************************************/
- /* Load the picture and define the source and dest rects. */
- /**********************************************************/
-
- pict = GetPicture( 128 );
- pictBounds = (**pict).picFrame;
- OffsetRect( &pictBounds, -pictBounds.left, -pictBounds.top );
-
- finalBounds = pictBounds;
- finalBounds.right = finalBounds.right / scaleRatio;
- finalBounds.bottom = finalBounds.bottom / scaleRatio;
-
- SizeWindow( gWindow, pictBounds.right + finalBounds.right,
- pictBounds.bottom, false );
- MoveWindow( gWindow, WLEFT( pictBounds.right + finalBounds.right ),
- WTOP( pictBounds.bottom ), false );
- ShowWindow( gWindow );
-
- /********************************************************/
- /* Create a temporary offscreen used to store the pict. */
- /********************************************************/
-
- if (NewGWorld( &srcGWorld, 8, &pictBounds, GetCTable( 8 ), nil, 0 ) != noErr)
- ExitToShell();
-
- srcPixMap = GetGWorldPixMap( srcGWorld );
- LockPixels( srcPixMap );
-
- /********************************************************/
- /* Draw the picture into the temporary gworld & window. */
- /********************************************************/
-
- GetGWorld( &savedPort, &savedDevice );
- SetGWorld( srcGWorld, nil );
- DrawPicture( pict, &pictBounds );
-
- SetGWorld( savedPort, savedDevice );
- DrawPicture( pict, &pictBounds );
-
- /*************************************************/
- /* Now, compress the picture into a data buffer, */
- /* then dispose the temporary gworld. */
- /*************************************************/
-
- if (GetMaxCompressionSize( srcPixMap, &(**srcPixMap).bounds, 8,
- codecMaxQuality, 'raw ', bestSpeedCodec, &size ))
- ExitToShell();
-
- imageData = NewPtr( size );
- desc = NewHandle( 1 );
-
- if (CompressImage( srcPixMap, &(**srcPixMap).bounds, codecMaxQuality,
- 'raw ', desc, imageData ))
- ExitToShell();
-
- DisposeGWorld( srcGWorld );
-
- /**********************************************************/
- /* Decompress the data buffer to the window with scaling. */
- /**********************************************************/
-
- OffsetRect( &finalBounds, pictBounds.right, 0 );
-
- for (i = 0; i < scaleRatio; i++)
- {
- DecompressImage( imageData, desc, (*(CWindowPtr)gWindow).portPixMap,
- &pictBounds, &finalBounds, srcCopy + ditherCopy, nil );
-
- OffsetRect( &finalBounds, 0, finalBounds.bottom - finalBounds.top );
- }
- }